package passengers.demo;

import java.util.regex.Pattern;

import presence.demo.R;
import database.demo.PassengersOperations;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

/**
 * editing passenger's details
 */
public class EditPassMainActivity extends Activity {
	private PassengersOperations passengerDBoperation;
	TextView tmpID;
	EditText tmpLastName;
	EditText tmpFirstName;
	EditText tmpPhone;
	EditText tmpEmail;
	
	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile(
            "[a-zA-Z0-9+._%-+]{1,256}" +
            "@" +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,64}" +
            "(" +
            "." +
            "[a-zA-Z0-9][a-zA-Z0-9-]{0,25}" +
            ")+"
        );
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edit_pass);	
		passengerDBoperation = new PassengersOperations(this);
		passengerDBoperation.open();
		findViewById(R.id.edit_last).requestFocus();
		addButtonListeners();
		
		tmpID = (TextView) findViewById(R.id.edit_id);
		tmpLastName = (EditText) findViewById(R.id.edit_last);
		tmpFirstName = (EditText) findViewById(R.id.edit_first);
		tmpPhone = (EditText) findViewById(R.id.edit_phone);
		tmpEmail = (EditText) findViewById(R.id.edit_mail);
		
		
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
		    String value = extras.getString("ID");
		    tmpID.setText(value);
		    Cursor cursor = passengerDBoperation.getPass(value);
		    tmpLastName.setText(cursor.getString(1));
		    tmpFirstName.setText(cursor.getString(2));
		    tmpPhone.setText(cursor.getString(3));
		    tmpEmail.setText(cursor.getString(4));
		    
		}
		
		
	}
	
	public void addButtonListeners() {	 
		Button addButtton = (Button) findViewById(R.id.btn_edit);
		tmpID = (TextView) findViewById(R.id.edit_id);
		tmpLastName = (EditText) findViewById(R.id.edit_last);
		tmpFirstName = (EditText) findViewById(R.id.edit_first);
		tmpPhone = (EditText) findViewById(R.id.edit_phone);
		tmpEmail = (EditText) findViewById(R.id.edit_mail);
		addButtton.setOnClickListener(new OnClickListener() {
		@Override
		public void onClick(View view) {
			if((tmpLastName.getText().toString().length()==0)||(tmpFirstName.getText().toString().length()==0))
			{
				Toast.makeText(EditPassMainActivity.this,"Please enter passenger name", Toast.LENGTH_LONG).show();
			}
			else if(checkEmail(tmpEmail.getText().toString()))
            {
            	updateUser(tmpID,tmpLastName,tmpFirstName,tmpPhone,tmpEmail);	
            }
            else
            {
               Toast.makeText(EditPassMainActivity.this,"Invalid Email Addresss", Toast.LENGTH_LONG).show();
            }
			
		}
		}); 		
	}
	
	
	private boolean checkEmail(String email) {
		if(email.length()==0){
			return true;
		}
		else return EMAIL_ADDRESS_PATTERN.matcher(email).matches();
    }
	
	
	public void updateUser(TextView tmpID,EditText tmpLastName, EditText tmpFirstName, EditText tmpPhone, EditText tmpEmail) {
		
		passengerDBoperation.updatePassengerDetails(tmpID.getText().toString(), tmpLastName.getText().toString(),
				tmpFirstName.getText().toString(), tmpPhone.getText().toString(),tmpEmail.getText().toString());
			Toast.makeText(EditPassMainActivity.this,"Updated!", Toast.LENGTH_LONG).show();
			EditPassMainActivity.this.finish();
			
		
	}
	
	
}












